﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using A2DFramework.InstanceService;
using A2DFramework.LogService.LogProviders;

namespace A2DFramework.LogService.Configuration
{
    public static class LogConfiguration
    {
        public static bool LogEnabled{get;set;}
        public static int LogFileSize{get;set;}
        public static string LogFileName { get; set; }

        public static string UdpServer { get; set; }
        public static int UdpPort { get; set; }

        public static LogType LogType { get; set; }

        static LogConfiguration()
        {
            XElement root = XElement.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A2D.config"));

            root = root.Element("LogConfig");
            if (root == null)
                throw new Exception("LogConfig node not exists");

            if (root.Attribute("LogType")==null)
                throw new Exception("LogConfig's LogType attribute not exists");

            if (root.Attribute("LogType").Value != "TextFile" && root.Attribute("LogType").Value != "Queue" && root.Attribute("LogType").Value != "Udp")
                throw new Exception("LogConfig's LogType must be TextFile or Queue or Udp");

            if (string.Compare(root.Attribute("LogType").Value, "TextFile", true)==0)
            {
                LogType = Configuration.LogType.TextFile;

                LogEnabled = bool.Parse(root.Element("TextFileLogProvider").Element("LogEnabled").Value);
                LogFileSize = int.Parse(root.Element("TextFileLogProvider").Element("LogFileSize").Value);
                if (root.Element("TextFileLogProvider").Element("LogFileName") != null)
                    LogFileName = root.Element("TextFileLogProvider").Element("LogFileName").Value;
            }
            else if (string.Compare(root.Attribute("LogType").Value, "Queue", true)==0)
            {
                LogType = Configuration.LogType.Queue;
            }
            else if (string.Compare(root.Attribute("LogType").Value, "Udp", true)==0)
            {
                LogType = Configuration.LogType.Udp;

                UdpServer = root.Element("UdpLogProvider").Element("UdpServer").Value;
                UdpPort = int.Parse(root.Element("UdpLogProvider").Element("UdpPort").Value);
            }
        }
    }
}
